(function() {
    angular.module("webApp").controller("MsgsetCtrl", MsgsetCtrl);
    MsgsetCtrl.$inject = ['$scope', 'dataService', 'helper', '$state', '$location', '$rootScope', 'sessionService'];

    function MsgsetCtrl($scope, dataService, helper, $state, $location, $rootScope, sessionService) {
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getMsgeventsList(1);
        }
        $scope.getMsgeventsList = function(pageNo) {
            var query = {
                page_no: pageNo,
                page_size: $scope.pageSize,
                store_id: $rootScope.store_id
            };
            dataService.msgsetGet(query).then(function(result) {
                if (result.total_count) {
                    $scope.events_users_list = result.events_users_list;
                    $scope.total_count = result.total_count;
                }
            });
        }

        //新建和编辑配置页面
        $scope.addAction = function() {
            $scope.msg_id = $location.search().msg_id;
            if ($scope.msg_id) {
                var query = {
                    "store_id": $rootScope.store_id,
                    "msg_id": $scope.msg_id
                };
                dataService.msgsetGet(query).then(function(result) {
                    if (result.total_count) {
                        $scope.msg_events_users = result.events_users_list[0];
                    } else {
                        helper.myAlert("error");
                    }
                });
            } else {
                $scope.msg_events_users = {};

                $scope.msg_events_users.store_id = $rootScope.store_id;
                $scope.msg_events_users.partner_id = sessionService.get("tokenUser").CompanyId;

                $scope.msg_events_users.role_id = 0;
                $scope.msg_events_users.user_id = 0;
                $scope.msg_events_users.item = 0;
                $scope.msg_events_users.baoming = 0;
                $scope.msg_events_users.balance = 0;
                $scope.msg_events_users.mianfu = 0;
                $scope.msg_events_users.hotel = 0;
                $scope.msg_events_users.tongji = 0;
                $scope.msg_events_users.kefu = 0;
                $scope.msg_events_users.refund = 0;
                $scope.msg_events_users.distributor = 0;
                $scope.msg_events_users.gendan = 0;
            }
        }

        $scope.msgsetAdd = function() {
            if (!$scope.msg_events_users.role_id) {
                helper.myAlert("请先选择角色");
                return false;
            }
            if (!$scope.msg_events_users.user_id) {
                helper.myAlert("请先选择用户");
                return false;
            }

            dataService.msgsetAdd($scope.msg_events_users).then(function(result) {
                if (result.Msg != "ok") {
                    helper.myAlert("error:" + result.Msg);
                } else {
                    helper.myAlert("success", null, null, function() {
                        $state.go("msgset.list");
                    });
                }
            });
        }

        //返回选中角色数据
        $scope.roledata = function(roleInfo) {
            $scope.roleInfo = roleInfo;
            $scope.msg_events_users.role_id = $scope.roleInfo.role_id;
        }

        //返回选中用户数据
        $scope.userdata = function(userInfo) {
            $scope.userInfo = userInfo;
            $scope.msg_events_users.user_id = $scope.userInfo.user_id;
        }
    }
})();